package org.example.v5.statemachine;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.logging.Logger;

/**
 * StateMachine 类管理状态的当前状态，并处理状态转换。
 * @param <S> 状态类型
 * @param <E> 事件类型
 */
@Getter
@Setter
@ToString
public class StateMachine<S, E extends Enum<E>> {
    private static final Logger LOGGER = Logger.getLogger(StateMachine.class.getName());

    /**
     * 当前状态处理器。
     */
    private StateAction<StateMachine<S, E>, E> stateAction;

    public StateMachine(StateAction<StateMachine<S, E>, E> initialStateAction) {
        // 初始化状态处理器
        this.stateAction = initialStateAction;
    }

    /**
     * 处理状态转换的事件。
     * @param event 触发事件
     */
    public void onEvent(E event) {
        if (stateAction != null) {
            stateAction.handle(this, event);
        }
    }

    /**
     * 设置状态处理器。
     * @param stateAction 状态处理器
     */
    public void setStateAction(AbstractStateAction<StateMachine<S, E>, E> stateAction) {
        this.stateAction = stateAction;
    }
}
